<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/8/12
 * Time: 4:09 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.modeladmin');
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.file');

require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/tailieu.php';

class VastModelTailieu extends JModelAdmin
{
    /**
     * @var		string	The prefix to use with controller messages.
     * @since	1.6
     */
    protected $text_prefix = 'COM_VAST';

    /**
     * Method to test whether a record can be deleted.
     *
     * @param	object	$record	A record object.
     *
     * @return	boolean	True if allowed to delete the record. Defaults to the permission set in the component.
     * @since	1.6
     */
    protected function canDelete($record)
    {
        if (!empty($record->id)) {
            $user = JFactory::getUser();
            return $user->authorise('vast.tailieu.delete', 'com_vast');
        }
    }

	/**
	 * Method to save the form data.
	 *
	 * @param   array  $data  The form data.
	 *
	 * @return  boolean  True on success, False on error.
	 *
	 * @since   11.1
	 */
	public function save($data)
	{
	   
        $file = JRequest::getVar('file_upload', null, 'files', 'array'); 
        
        if(isset($file)) { 
            //Clean up filename to get rid of strange characters like spaces etc
            $filename = JFile::makeSafe($file['name']);
 
            if($file['size'] > 1024 * 1024) return false;
            //Set up the source and destination of the file
 
            $src = $file['tmp_name'];
            
            $url = 'images' . DS . 'com_vast' . DS . $filename;
            $dest = JPATH_SITE . DS . 'images' . DS . 'com_vast' . DS . $filename;

            //First check if the file has the right extension, we need jpg only
            if ($file['type'] == 'application/pdf' 
                || $file['type'] == 'application/msword' 
                || $file['type'] == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                || $file['type'] == 'application/vnd.ms-powerpoint'
                || $file['type'] == 'application/vnd.openxmlformats-officedocument.presentationml.presentation') { 
                if ( JFile::upload($src, $dest) ) {
                    $data['dinhkem'] = $url;
                } else {
                    //Redirect and throw an error message
                    $this->setError(JText::_('COM_VAST_ERROR_IN_UPLOAD'));
                    return false;
                }
            } else {
                //Redirect and notify user file is not right extension
                $this->setError(JText::_('COM_VAST_FILE_TYPE_INVALID'));
                return false;
            } 

        }

        return parent::save($data);
    }
    
    /**
     * Returns a Table object, always creating it.
     *
     * @param	type	The table type to instantiate
     * @param	string	A prefix for the table class name. Optional.
     * @param	array	Configuration array for model. Optional.
     *
     * @return	JTable	A database object
     */
    public function getTable($type = 'Tailieu', $prefix = 'JTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    /**
     * Method to get the record form.
     *
     * @param	array	$data		Data for the form.
     * @param	boolean	$loadData	True if the form is to load its own data (default case), false if not.
     *
     * @return	mixed	A JForm object on success, false on failure
     * @since	1.6
     */
    public function getForm($data = array(), $loadData = true)
    {
        // Get the form.
        $form = $this->loadForm('com_vast.tailieu', 'tailieu', array('control' => 'jform', 'load_data' => $loadData));
        if (empty($form)) {
            return false;
        }
        $jinput = JFactory::getApplication()->input;

        // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
        if ($jinput->get('a_id'))
        {
            $id =  $jinput->get('a_id', 0);
        }
        // The back end uses id so we use that the rest of the time and set it to 0 by default.
        else
        {
            $id =  $jinput->get('id', 0);
        }

        $user = JFactory::getUser();
        
        return $form;
    }

    /**
     * Method to get the data that should be injected in the form.
     *
     * @return	mixed	The data for the form.
     * @since	1.6
     */
    protected function loadFormData()
    {
        // Check the session for previously entered form data.
        $data = JFactory::getApplication()->getUserState('com_vast.edit.tailieu.data', array());

        if (empty($data)) {
            $data = $this->getItem();

        }

        return $data;
    }
}